/**
 * Created by huotaol on 2017/4/26.
 */
import React, {Component} from "react";
import {Text, TouchableOpacity, View} from "react-native";
import CustomStyle from "../../../../common/style/CustomStyle.js";
import SectionTitle from "./SectionTitle";
import GiftedListView from "react-native-gifted-listview";
import {connect} from "react-redux";
import {request} from "../../../../common/util/RequestUtil";
import * as BaseUrl from "../../../constant/Url";
import {Actions} from "react-native-router-flux";

class HomePageNews extends Component {
    static propTypes = {};
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {};
    }

    render() {
        return (
            <View >
                <SectionTitle title='新闻资讯' rightText='查看更多' rightClick={() => Actions.NewsUI()} showArrow={true}/>
                <GiftedListView
                    style={{
                        paddingLeft: CustomStyle.getActualSizeInHorizontal(20),
                        paddingRight: CustomStyle.getActualSizeInHorizontal(20),
                    }}
                    onFetch={this._onFetch}
                    rowView={this._renderRowView}
                    initialListSize={10} // the maximum number of rows displayable without scrolling (height of the listview / height of row)
                    firstLoader={true} // display a loader for the first fetching
                    pagination={false} // enable infinite scrolling using touch to load more
                    refreshable={false} // enable pull-to-refresh for iOS and touch-to-refresh for Android
                    withSections={false} // enable sections
                    renderSeparator={this._renderSeparator}
                    rowHasChanged={(r1, r2) => {
                        r1.id !== r2.id
                    }}
                />
            </View>
        );
    }

    _renderSeparator() {
        return <View style={[CustomStyle.separator, {}]}/>
    }

    /**
     * Render a row
     * @param {object} rowData Row data
     */
    _renderRowView(rowData) {
        return (
            <TouchableOpacity
                style={{height: CustomStyle.getActualSizeInVertical(68), justifyContent: 'center',}}
                onPress={() => {
                    Actions.WebViewPage(rowData);
                }}
            >
                <Text
                    numberOfLines={1}
                    style={[CustomStyle.baseText, {
                        fontSize: CustomStyle.getActualFontSize(28),
                        color: '#4a3d18',
                        textAlignVertical: 'center',
                    }]}>{rowData.title}</Text>
            </TouchableOpacity>
        );
    }

    _onFetch(page = 1, callback, options) {
        const response = BaseUrl.BASE_URL + '/news/m/get_top_list';
        request(response).then(response => {
            const {data} = response;
            const newsList = data.list.map(item => {
                const id = item.newsid;
                return {
                    url: `http://m.chehangjia.cn/module/newsdetail.html?newsid=${id}&type=0&show=1`,
                    title: item.newstitle,
                };
            });
            callback(newsList);
        }).catch(error => {

        });
    }
}

export default connect()(HomePageNews);
